package edu.uhcl.team_drone.world;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.IntAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.Array;
import edu.uhcl.team_drone.assets.Assets;
import edu.uhcl.team_drone.game.ProjectGame;

public class GameWorld {

    private Model model; // Model used to store the flat plane floor 

    Array<ModelInstance> instances = new Array<ModelInstance>();

    private Environment environment;

    public GameWorld() {

        ModelBuilder modelBuilder = new ModelBuilder();

        // make floor
        model = Assets.manager.get("GrassPlane_Better.g3db", Model.class);
        final int spacing = 1790;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 8; j++) {
                instances.add(
                        new ModelInstance(
                                model,
                                ((-spacing * 4) + (i * spacing)),
                                -10,
                                ((-spacing * 4) + (j * spacing))));
            }

        }

        // make boxes
        model = modelBuilder.createBox(
                200, 500, 200,
                new Material(
                        ColorAttribute.createDiffuse(Color.GREEN)),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);

        for (int i = 0; i < 8; i++) {
            instances.add(new ModelInstance(model, 800 + (i * 400), 250, 0));
        }
        for (int i = 0; i < 8; i++) {
            instances.add(new ModelInstance(model, 800 + (i * 400), 250, 400));
        }

        // make lighting environment
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

        // make sybox
        model = Assets.manager.get("SKYBOX.g3db", Model.class);
        model.materials.get(0).set(new IntAttribute(IntAttribute.CullFace, 0));
        instances.add(new ModelInstance(model));        
    }

    public void render(Camera camIn, float dt) {
        ProjectGame.modelBatch.begin(camIn);
        ProjectGame.modelBatch.render(instances, environment);
        ProjectGame.modelBatch.end();
    }

    public void dispose() {
        model.dispose();
    }

}
